<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">


    <title>list_business</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <div th:replace="~{commons/script::script}"></div>
</head>

<body>
<div>
    <span>客户视图：</span><span onclick="return whole('system/customer/CustomerAction_pagination.action')" class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true">全部</span>|
    <span onclick="return myCustomer('/business/myCustomer')" class="easyui-linkbutton" data-options="plain:true">我的商机</span>|
    <span onclick="return subCustomer('/business/subCustomer')" class="easyui-linkbutton" data-options="plain:true">下属商机</span>
    &nbsp;&nbsp;&nbsp;
    <span onclick="return todayCreate('/business/todayCreate')" class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true">今日新建</span>|
    <span onclick="return weekCreate('/business/todayCreate')" class="easyui-linkbutton" data-options="plain:true">本周新建</span>|
    <span onclick="return monthCreate('/business/todayCreate')" class="easyui-linkbutton" data-options="plain:true">本月新建</span>|
    <span onclick="return followed('system/customer/CustomerAction_followed.action')" class="easyui-linkbutton" data-options="plain:true">我关注的</span>|
    <!--<span id="shared" class="easyui-linkbutton" data-options="plain:true">我共享的</span>|
    <span id="sharedTome" class="easyui-linkbutton" data-options="plain:true">共享给我的</span>-->
    &nbsp;&nbsp;&nbsp;
    <span id="recycleBin" onclick="return recycle('/business/recycle')" class="easyui-linkbutton" data-options="iconCls:'icon-recover_deleted',plain:true">回收站</span>
    &nbsp;
    <span id="advancedSearch" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">高级搜索</span>
</div>
<div id="tool" style="margin-bottom: 20px">
    <table>
        <tr>
            <td>
                <i class="iconfont"></i>

            </td>
            <td>
                <a href="javascript:void(0);" onclick="return del('/business/delete')" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">删除</a>
                <select id="searchBy" style="width:150px;height:25px;">
                    <option value="-1">--请选择筛选条件--</option>
                    <option value="names">按客户名查询</option>
                    <option value="bname">按商机名查询</option>
                    <option value="origin">按商机来源查询</option>
                    <option value="nextstep">按下一次联系内容查询</option>
                    <!-- 	<option value="owner.username">按负责人查询</option> -->
                    <!--	<option value="userByCreatoruserid">按创建人查询</option> -->
                </select>
                <select name="contain" style="width:60px;height:25px;">
                    <option value="-1">--包含--</option>
                </select>
            </td>
            <td  class="text-content">
                <input id="searchText" type="text" placeholder="请输入查询关键字" style="border:1px solid black;height:25px;">
                <span id="search" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">搜索</span>
                <span id="reset" class="easyui-linkbutton" data-options="iconCls:'icon-update',plain:true">重置</span>
            </td>
            <td>
                <span id="chat" class="easyui-linkbutton" data-options="iconCls:'icon-cut',plain:true">发送短信</span>
                <span id="email" class="easyui-linkbutton" data-options="iconCls:'icon-redo',plain:true">发送邮件</span>
            </td>

            <td>
                <span id="addBtn" onclick="return add('/business/toAddBusiness')" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">添加商机</span>
                <span id="tool1" class="easyui-linkbutton" data-options="iconCls:'icon-cog',plain:true">商机工具</span>
            </td>
        </tr>
    </table>
</div>
<div id="businessList"></div>
</body>

<script type="text/javascript">
    $("#search").on("click",function(event){
        var searchBy=$("#searchBy").val();
        var searchText=$("#searchText").val();
        $("#businessList").datagrid("load",{
            "searchBy":$("#searchBy").val(),
            "searchText":$("#searchText").val()
        })
    })
    $("#reset").on("click",function(event){
        $("#searchBy").val("-1");
        $("#searchText").val("");
    })


    function add(url){

          /*parent.parent.open({
            width:900,
            height:600,
            title:"添加商机",
            "url":"url",
            close:function(){
              $("#businessList").datagrid("reload");
            }
          });*/
        location.href=url;
    }


    $(function(){
        //分页展示客户列表
        $("#businessList").datagrid({
            url:"/business/businessList",
            fitColumns:true,
            checkOnSelect:false,
            idField:"name",
            pagination:true,
            sortName:"name",
            columns:[
                [
                    {field:"选择",checkbox:true},
                    {field:"id",hidden:true},
                    {field:"names",title:"客户",width:12},
                    {field:"bname",title:"商机名",width:12},
                    {field:"origin",title:"商机来源",width:10},
                    {field:"nextstepTime",title:"下一次联系时间",width:10},
                    {field:"nextstep",title:"下一次联系内容",width:10},
                    {field:"ownerUser",title:"负责人",width:5},
                    {field:"creatorUser",title:"创建人",width:5},
                    {field:"createTime",title:"创建时间",width:10},
                    {field:"updateTime",title:"更新时间",width:10},
                    {field:"operation",title:"操作",width:20,formatter:function(value,row,index){
                            return "<a href='javascript:void(0)' onclick='return see("+index+")'>查看</a>"+
                                " <a href='javascript:void(0)' onclick='return modify("+index+")'>修改</a>";
                        }}
                ]
            ],
            loadFilter:function(data){
                return {total : data.data.totalRows,rows:data.data.result}
            }
        })
    })
    function see(index){
        $("#businessList").datagrid("selectRow",index);
        var row=$("#businessList").datagrid("getSelected");
        parent.parent.openTopWindow({
            width:900,
            height:600,
            title:"查看商机",
            "url": "/business/see?id="+row.id,
            close:function(){
                $("#businessList").datagrid("reload");
            }
        });
    }
    function modify(index){
        $("#businessList").datagrid("selectRow",index);
        var row=$("#businessList").datagrid("getSelected");
        parent.parent.openTopWindow({
            width:900,
            height:600,
            title:"修改商机",
            "url": "/business/toEdit?id="+row.id,
            close:function(){
                $("#businessList").datagrid("reload");
            }
        });
    }

    //删除按钮事件处理函数
    function del(url){
        //获取到选中的一行数据
        var rows = $("#businessList").datagrid("getChecked");

        if(!rows.length){
            $.messager.alert("警告","请选择要删除的数据");
            return;
        }

        $.messager.confirm("警告","数据删除后无法恢复，是否确认删除",function(b){
            if(b){
                var ids = new Array();
                $.each(rows,function(index,obj){
                    ids.push(obj.id);
                });
                //将数组中元素使用,分割拼接为一个字符串
                ids = ids.join(",");
                $.post(url,
                    {
                        "ids" : ids
                    },
                    function(data){
                        if(data && data.success){
                            $.messager.alert("提示",data.message);
                            $("#businessList").datagrid("reload");
                            return;
                        }
                        $.messager.alert("警告",data.message);
                    },"json");
            }
        });
    }

    //查询今天创建的客户
    function todayCreate(url) {
        /*$("#businessList").datagrid({
            "url": url + "?type=day"
        })*/
        location.href=url + "?type=day";

    }

    //查询周创建的客户
    function weekCreate(url) {
        location.href=url + "?type=week";
    }

    //查询月创建的客户
    function monthCreate(url) {
        location.href=url + "?type=month";
    }
    function myCustomer(url) {
        location.href=url;
    }
    function subCustomer(url) {
        location.href=url;
    }
    function recycle(url) {
        location.href=url;
    }

    //邮件发送
    $("#email").on("click", function (e) {
        var rows = $("#businessList").datagrid("getSelections");
        if (!rows || rows.length == 0) {
            $.messager.alert("警告", "请选中要发送邮件的客户");
            return;
        }
        if(rows.length>1){
            $.messager.alert("警告","请选择单条数据进行发送邮件");
            return false;
        }
        var userId = rows[0].id;

        location.href = "/business/emails?id=" + userId;
    })

</script>
</body>
</html>